rvPlayer = function () {

	var w;
	var PLAYER_SERVER_PORT = 9449;

	var ws;
	var watchdog;
	
	this.init = function() {
		ws = new rvWebServer(PLAYER_SERVER_PORT, onRequest);
		ws.start();
		
		watchdog = new rvWatchdog();
		watchdog.start(reloadViewer);
		
		if (!$rv.debug) {
			startViewerWithDelay();
		}
	};
	
	var log = function(msg) {
		console.log(msg);
	};
	
	var onRequest = function(socketId, cmd, qs, keepAlive) {
		log("Player request: cmd=" + cmd + " | qs=" + qs);
		
		try {
	        
	        if (cmd === "/ping") {
	        	var cb = ws.getUrlParam(qs, "callback");
				cb = cb ? cb + "();" : "";
				ws.writeTextResponse(socketId, cb, keepAlive, ws.CONTENT_TYPE_JAVASCRIPT);

	        } else if (cmd === "/heartbeat") {
				watchdog.poke();
	        	var cb = ws.getUrlParam(qs, "callback");
				cb = cb ? cb + "();" : "";
				ws.writeTextResponse(socketId, cb, keepAlive, ws.CONTENT_TYPE_JAVASCRIPT);
	        	
	        } else if (cmd === "/set_property") {
	    		if ("true" === (ws.getUrlParam(qs, "reboot_required"))) {
	    			log("reboot_required received");
	    			reboot();
	    		} else if ("true" === (ws.getUrlParam(qs, "restart_required"))) {
	    			log("restart_required received");
	    			restart();
	    		} else if ("true" === (ws.getUrlParam(qs, "update_required"))) {
	    			log("update_required received");
	    			//Utils.restart(); 
	    		} else	if ("true" === (ws.getUrlParam(qs, "reboot_enabled"))) {
	    			//Config.setRestartTime(queryMap.get("reboot_time")); //time to restart (not reboot)
	    		}

				ws.writeTextResponse(socketId, "", keepAlive, ws.CONTENT_TYPE_TEXT_PLAIN);
	        } else if (cmd === "/save_property") {
	            
	    		var displayId = ws.getUrlParam(qs,"display_id");
	    		var claimId = ws.getUrlParam(qs,"claim_id");
	    		var server = ws.getUrlParam(qs,"server");
	    		var restartViewerParam = ws.getUrlParam(qs,"restart_viewer");
	    		
	    		if (displayId) {
	    			$rv.config.displayId = displayId;
	    		}
	    		
	    		if (claimId) {
	    			$rv.config.claimId = claimId;
	    		} 

	    		if (server) {
	    			$rv.config.server = server;
	    		} 

	    		$rv.config.saveDisplayProperties(function() {
		    		if ("true" === restartViewerParam) {
		    			restartViewer();
		    		} 
	    		});
				ws.writeTextResponse(socketId, "", keepAlive, ws.CONTENT_TYPE_TEXT_PLAIN);
	    		
	        } else if (cmd === "/clear") {
	    		$rv.config.clearStorage();
				ws.writeTextResponse(socketId, "", keepAlive, ws.CONTENT_TYPE_TEXT_PLAIN);
			} else if (cmd === "/restart") {
				log("restart command received");
				restart();
			} else if (cmd === "/reboot") {
				log("reboot command received");
				reboot();
	        } else if (cmd === "/shutdown") {
				log("shutdown command received");
				ws.writeTextResponse(ws, socketId, "", keepAlive, ws.CONTENT_TYPE_TEXT_PLAIN);
				shutdown();
	        } else if (cmd === "/config") {
	        	var configHTML = rvGetConfigPageHtml($rv.config.displayId, $rv.config.claimId, $rv.config.server)
				ws.writeTextResponse(socketId, configHTML, keepAlive, ws.CONTENT_TYPE_TEXT_HTML);
	        } else if (cmd === "/version") {
	        	var appInfo = chrome.runtime.getManifest();
				ws.writeTextResponse(socketId, appInfo.version, keepAlive, ws.CONTENT_TYPE_TEXT_PLAIN);
	        } else {
				ws.writeErrorResponse(socketId, keepAlive, ws.HTTP_BAD_REQUEST_TEXT);
	        }
		} catch (e) {
    		log("onRequest error" + e.message);
			ws.writeErrorResponse(socketId, keepAlive, ws.HTTP_BAD_REQUEST_TEXT);
		}
        
	};
		
	var shutdown = function() {
		ws.stop();
		$rv.close();
	};

	var startViewerWithDelay = function() {
		setTimeout(startViewer, 3000);
	};

	var startViewer = function() {
		$rv.browser.src = $rv.config.getViewerUrl();
	};

	var restartViewer = function() {
		if ($rv.browser.src === $rv.config.getViewerUrl()) {
			$rv.browser.reload();
		} else {
			$rv.browser.src = $rv.config.getViewerUrl();
		}
	};

	var stopViewer = function() {
		$rv.browser.stop();
	};
	
	var reloadViewer = function() {
		$rv.browser.reload();
	};

	var restart = function() {
		reloadViewer();
	};
	
	var reboot = function() {
		chrome.runtime.reload();
	};

};

